package edu.ccut.saturn.studio.logic.model.saturn;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.graphics.Image;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.dialog.decisiondialog.SaturnDecisionDialog;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.logic.figure.SaturnFigure;
import edu.ccut.saturn.studio.logic.model.Shape;

public class SaturnDecisionNode extends Shape {

	private static final long serialVersionUID = -1617071170625969906L;

	public final static String KEY_NODE_DECISION = "decision"; 

	public final static String SHOW_NODE_DECISION = Messages.getString("SaturnDecisionNode.choose"); //$NON-NLS-1$
	
	public static final String KEY_DECISION_NODE_NAME = "_property_name"; 

	public static final String KEY_DECISION_NODE_DESCRITPION = "_property_description"; 

	public static final String KEY_DECISION_NODE_IF = "_property_if"; 

	public static final String KEY_DECISION_NODE_IF_ATTRIBUTE_THEN = "_property_then"; 

	public static final String KEY_DECISION_NODE_IF_ATTRIBUTE_ELSE = "_property_else"; 

	public static final String KEY_DECISION_NODE_IF_EXPR = "_property_expr"; 

	public static final String SHOW_DECISION_NODE_NAME = "name"; 

	public static final String SHOW_DECISION_NODE_DESCRITPION = "description"; 

	public static final String SHOW_DECISION_NODE_IF = "if"; 

	public static final String SHOW_DECISION_NODE_IF_ATTRIBUTE_THEN = "then"; 

	public static final String SHOW_DECISION_NODE_IF_ATTRIBUTE_ELSE = "else"; 

	public static final String SHOW_DECISION_NODE_IF_EXPR = "expr"; 

	private String thenId = ""; 

	private String falseId = "";// else 

	private String expression = ""; 

	private Image image=Activator.getImage(Images.DECISION_ICON);

	private static int number = 1;
	
	public String getFalseId() {
		return falseId;
	}

	public void setFalseId(String falseId) {
		if(!this.falseId.equals(falseId)){
			this.falseId = falseId;
			firePropertyChange(PROPERTERY_CHANGS, null, falseId);
		}
		
	}

	public SaturnDecisionNode() {
		super.setName(SHOW_NODE_DECISION + number++);
	}

	public String getExpression() {
		return expression;
	}

	public void setExpression(String expression) {
		if(!this.expression.equals(expression)){
			this.expression = expression;
			firePropertyChange(PROPERTERY_CHANGS, null, expression);
		}
	}

	public String getThenid() {
		return thenId;
	}

	public void setThenid(String thenid) {
		if(!this.thenId.equals(thenid)){
			this.thenId = thenid;
			firePropertyChange(PROPERTERY_CHANGS, null, thenid);
		}
	}

	@Override
	public IFigure getFigure() {

		SaturnFigure saturnFigure = new SaturnFigure();
		saturnFigure.setIcon(image);
		saturnFigure.setName(super.getName());

		return saturnFigure;
	}

	@Override
	public Image getIcon() {
		return image;
	}

	@Override
	public Node doRefresh() {
		Node root = super.getRoot();

		Node decisionNode = SaturnW3CUtil.createChildNode(root, KEY_NODE_DECISION, null);
		SaturnW3CUtil.createAttribute(decisionNode,
				Shape.KEY_ATTRIBUTE_SHAPE_NEXT_ID, ""); 
		SaturnW3CUtil.createChildNode(decisionNode,
				SHOW_DECISION_NODE_NAME, super.getName());

		Node ifNode = SaturnW3CUtil.createChildNode(decisionNode, SHOW_DECISION_NODE_IF, null);

		SaturnW3CUtil.createAttribute(ifNode,
				SHOW_DECISION_NODE_IF_ATTRIBUTE_THEN, this.thenId);

		SaturnW3CUtil.createAttribute(ifNode,
				SHOW_DECISION_NODE_IF_ATTRIBUTE_ELSE, this.falseId);

		SaturnW3CUtil.createChildNode(ifNode,
				SHOW_DECISION_NODE_IF_EXPR, this.expression);
		
		super.createParemeters(decisionNode);

		
		return decisionNode;
	}

	@Override
	public Dialog getDialog() {
		SaturnDecisionDialog dialog = new SaturnDecisionDialog(super.getShell(),
				this);
		super.reorder(this);
		return dialog;
	}

	public String getKey() {
		return this.expression;
	}

	public void setKey(String key) {
		if(!this.expression.equals(key)){
			this.expression = key;
			firePropertyChange(PROPERTERY_CHANGS, null, key);
		}
		
	}

	@Override
	public Shape cloneShape(Point mousePoint, Shape shape) {
		return null;
	}
	@Override
	protected void setImage(Image image) {
		this.image = image;
	}

	@Override
	public Parameters createNodeHtml(String filePath, String[] tempnames) {
		
		Map<String, String> infos = new HashMap<String, String>();
		String name = getName();
		Parameters para = new Parameters();

	 	infos.put("name", getName());
	 	infos.put("id", getId());
	 	infos.put("expression", getKey());
	 	infos.put("description", getDescription());
	 	
		para.setInfourl( name + "-decision.html");
		para.setInfoname( name );
		para.setDescription(getDescription());
		para.setNodeType(Messages.getString("Logic.ChooseNode")); //$NON-NLS-1$
		para.setImagename("decision");
		String templatePath = "template/export/logic/decision";
		String Path = filePath + File.separator + name + "-decision.html";
		FreemarkerUtils.writeTemplateToFile(templatePath,infos, Path);
		
		return para;
	}
}
